﻿
#include "qloggerappender.hpp"
#include "qloggerengine.hpp"
#include "qlogger_.hpp"

void QLoggerAppender::streamWrite (QTextStream &stream, const QLoggerEvent &event)
{
    stream << format(event);
    stream.flush();
}

QString QLoggerAppender::format(const QLoggerEvent &event)
{
    QString message = QString(_format);
    message.replace("%d", event.dateTime.toString(_time_format));
    message.replace("%r", QString("%1").arg(QLoggerEngine::handle().elapsed()));
    message.replace("%l", QLoggerEngine::levelName(event.level));
    message.replace("%c", event.logger->name);
    message.replace("%i", QString("%1").arg(event.line));
    message.replace("%f", QString(event.file));
    message.replace("%p", QString(event.func));
    message.replace("%m", *(event.message));
    message.replace("%n", "\n");

    return message;
}

QLoggerAppender::QLoggerAppender (const QString &format, int level, const QString& time_format) :
    _format(format),
    _time_format(time_format),
    _level(static_cast<QLoggerLevel>(level)),
    _codec(0)
{
    if (format.size() == 0)
        _format = "%d - <%l> - {%c} - %m%n";
}

QLoggerLevel QLoggerAppender::level ()
{
    return _level;
}

void QLoggerAppender::setCodec(QTextCodec *c)
{
    _codec = c;
}
